package com.three.usermanagement.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.three.core.entity.User;
import com.three.core.vo.R;
import com.three.usermanagement.mapper.UserMapper;
import com.three.usermanagement.service.UserService;
import com.three.usermanagement.utils.Upload;
import com.three.usermanagement.vo.UpdatePasswordVo;
import com.three.usermanagement.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
@RestController
@RequestMapping("/userManagement/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;
@RequestMapping("/selectByPage")
    public R<Page<User>> getByName(@RequestBody UserVo userVo){
    return userService.selectUserByPage(userVo);
    }
    @PostMapping("/update")
    public R update(@RequestParam String username, @RequestBody User user){
        user.setUpdateBy(username);
        user.setUpdateTime(LocalDateTime.now());
        return userService.updateUser(user);
    }
    @PostMapping("/delete")
    public R delete(@RequestBody User user){
        return userService.deleteUser(user);
    }
    @PostMapping("/addUser")
    public R addUser(@RequestParam String username,@RequestBody User user){
        R insert = userService.insert(user);
        user.setCreateBy(username);
        user.setCreateTime(LocalDateTime.now());
        if (insert != null){
            return new R(200,"添加成功",null);
        }else{
            return new R(500,"添加失败",null);

        }
    }
    //上传图片
    @PostMapping("/upload")
    public R uploadFile(MultipartFile file) {
        try {
            String url = Upload.uploadFile(file);
            return new R(200,"上传成功",url);
        } catch (Exception e) {
            return new R(500,"上传失败",null);
        }
    }
    @PostMapping("/checkPass")
    public R checkPass(@RequestBody UpdatePasswordVo updatePasswordVo){
        return userService.checkPass(updatePasswordVo);
    }
    @PostMapping("/modifyPass")
    public R modifyPass(@RequestBody UpdatePasswordVo updatePasswordVo){
        return userService.updateUserPass(updatePasswordVo);
    }
}
